package xc.com.course.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import xc.com.common.Result;
import xc.com.course.domain.CoursePic;
import xc.com.course.feign.OssFegin;
import xc.com.course.service.CoursePicService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhuolizhong
 * @since 2021-06-03
 */
@RestController
@RequestMapping("/course-pic")
@CrossOrigin
@Api(tags = "coursePic接口",description = "课程图片信息的增删改查")
public class CoursePicController {
  @Autowired
    private CoursePicService coursePicService;
  @Autowired
  private OssFegin ossFegin;

  /**
   * 添加课程图片信息
   * @param coursePic
   * @return
   */
  @PostMapping("/add")
  @ApiOperation("添加课程图片信息")
    public Result<String> addCoursePic(@RequestBody CoursePic coursePic){
      Result<String> addCoursePic = coursePicService.addCoursePic(coursePic);
      return addCoursePic;
  }

  /**
   * 把课程图片上传到OSS存储空间
   * @param id
   * @param file
   * @return
   */
  @PostMapping("/upload/{id}")
  @ApiOperation("把课程图片上传到OSS存储空间")
  public Result<String> upload(@PathVariable("id")String id,MultipartFile file) {
    CoursePic coursePic=new CoursePic();
    String img = ossFegin.upload(file);
    coursePic.setCourseid(id);
    coursePic.setPic(img);
    return this.addCoursePic(coursePic);
  }

  /**
   * 根据课程id查询课程图片信息
   * @param courseId
   * @return
   */
  @GetMapping("/selectById/{courseId}")
  public CoursePic selectById(@PathVariable("courseId") String courseId){
    return coursePicService.selectById(courseId);
  }



}

